package com.xdtech.transportation.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.xdtech.transportation.service.TranDriverService;
import com.xdtech.transportation.vo.TranDriverItem;
import com.xdtech.web.model.Pagination;
import com.xdtech.web.model.ResultMessage;

/**
 * 
 * @author max.zheng
 * @create 2015-03-17 14:04:31
 * @since 1.0
 * @see
 */
@Controller
@Scope("prototype")
public class TranDriverController {
	@Autowired
	private TranDriverService tranDriverService;
	@RequestMapping(value="/tranDriver.do",params = "tranDriver")
	public ModelAndView skipTranDriver() {
		return new ModelAndView("transportation/tranDriver/tranDriver_ftl");
	}
	
	@RequestMapping(value="/tranDriver.do",params="loadList")
	@ResponseBody
	public Map<String, Object> loadList(HttpServletRequest request,Pagination pg) {
		Map<String, Object> results = null;
		if (StringUtils.isEmpty(request.getParameter("page"))) {
			//不分页处理
			results = tranDriverService.loadPageAndCondition(null, null);
		}else {
			results = tranDriverService.loadPageAndCondition(pg, null);
		}
		
		return results;
	}
	
	@RequestMapping(value="/tranDriver.do",params = "editTranDriver")
	public ModelAndView editTranDriver(HttpServletRequest request,Long tranDriverId) {
		if (tranDriverId!=null) {
			request.setAttribute("tranDriverItem", tranDriverService.loadTranDriverItem(tranDriverId));
		}
		return new ModelAndView("transportation/tranDriver/editTranDriver_ftl");
	}
	
	@RequestMapping(value="/tranDriver.do",params = "saveTranDriver")
	@ResponseBody
	public ResultMessage saveTranDriver(TranDriverItem item) {
		ResultMessage r = new ResultMessage();
		if (tranDriverService.saveOrUpdateTranDriver(item)) {
			r.setSuccess(true);
		}else {
			r.setSuccess(false);
		}
		return r;
	}
	
	@RequestMapping(value="/tranDriver.do",params = "deleteTranDriverItems")
	@ResponseBody
	public ResultMessage deleteTranDriverItems(String ids) {
		ResultMessage r = new ResultMessage();
		if (StringUtils.isNotEmpty(ids)) {
			String[] tempIds = ids.split(",");
			List<Long> tranDriverIds = new ArrayList<Long>();
			for (String id : tempIds) {
				tranDriverIds.add(Long.valueOf(id));
			}
			tranDriverService.deleteTranDriverInfo(tranDriverIds);
			r.setSuccess(true);
		}else {
			r.setSuccess(false);
		}
		return r;
	}

}
